<template>
	<div class="app-container">
		<el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
			label-width="98px">
			<div class="app-space-between">
				<div class="app-space-between-left">

					<el-form-item label="姓名:" prop="name">
						<el-input v-model="queryParams.name" placeholder="请输入姓名" clearable @clear="handleQuery"
							@keyup.enter.native="handleQuery" />
					</el-form-item>

					<el-form-item label="电话:" prop="phone">
						<el-input v-model="queryParams.phone" placeholder="请输入电话" clearable @clear="handleQuery"
							@keyup.enter.native="handleQuery" />
					</el-form-item>
				</div>
				<div class="app-space-between-right">
					<el-form-item>
						<el-button type="primary" size="mini" @click="handleQuery">搜索</el-button>
						<el-button size="mini" @click="resetQuery">重置</el-button>
					</el-form-item>
				</div>
			</div>
		</el-form>

		<el-row :gutter="10" class="mb8">
			<el-col :span="1.5">
				<el-button type="primary" plain size="mini" @click="handleAdd" v-hasPermi="['system:post:add']">新增
				</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="primary" plain size="mini" :disabled="single" @click="handleUpdate">编辑</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="primary" plain size="mini" :disabled="multiple" @click="handleDelete">删除</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="primary" plain size="mini" @click="handleImport">导入</el-button>
			</el-col>
			<right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
		</el-row>

		<el-table v-loading="loading" :data="postList" @selection-change="handleSelectionChange">
			<el-table-column type="selection" width="55" align="center" />
			<el-table-column label="姓名" align="center" prop="name" width="200" />
			<el-table-column label="电话" align="center" prop="phone" />
		</el-table>

		<pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
			@pagination="getList" />

		<el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
			<el-form ref="form" :model="form" :rules="rules" label-width="100px" v-loading="diLogloading">
				<el-form-item label="姓名:" prop="name">
					<el-input v-model="form.name" placeholder="请输入姓名" />
				</el-form-item>
				<el-form-item label="电话:" prop="phone">
					<el-input v-model="form.phone" placeholder="请输入电话" />
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer" v-if="!diLogloading">
				<el-button type="primary" :loading="btnLoading" @click="submitForm">{{form.configId?'保 存':'确 定'}}
				</el-button>
				<el-button @click="cancel" :disabled="btnLoading">取 消</el-button>
			</div>
		</el-dialog>



		<!-- 用户导入对话框 -->
		<el-dialog :title="upload.title" :visible.sync="upload.open" width="400px" append-to-body>
			<el-upload ref="upload" :limit="1" accept=".xlsx, .xls" :headers="upload.headers" :action="upload.url"
				:disabled="upload.isUploading" :on-progress="handleFileUploadProgress" :on-success="handleFileSuccess"
				:auto-upload="false" drag>

				<i class="el-icon-upload"></i>
				<div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
				<div class="el-upload__tip text-center" slot="tip">
					<div class="el-upload__tip" slot="tip">
						<el-checkbox v-model="upload.updateSupport" /> 是否更新已经存在的用户数据
					</div>
					<span>仅允许导入xls、xlsx格式文件。</span>
					<el-link type="primary" :underline="false" style="font-size:12px;vertical-align: baseline;"
						@click="importTemplate">下载模板</el-link>
				</div>
			</el-upload>
			<div slot="footer" class="dialog-footer">
				<el-button type="primary" @click="submitFileForm">确 定</el-button>
				<el-button @click="handleUploadClose">取 消</el-button>
			</div>
		</el-dialog>
	</div>
</template>

<script>
	import {
		listAdd,
		listPages,
		listUpdate,
		listDelete,
		listDetails,
		listImportData
	} from "@/api/wxechat/InternalPersonnel";
	import {
		getToken
	} from "@/utils/auth";
	export default {
		data() {
			return {
				// 遮罩层
				loading: true,
				// 选中数组
				ids: [],
				// 非单个禁用
				single: true,
				// 非多个禁用
				multiple: true,
				// 显示搜索条件
				showSearch: true,
				// 总条数
				total: 0,
				// 岗位表格数据
				postList: [],
				// 弹出层标题
				title: "",
				// 是否显示弹出层
				open: false,
				diLogloading: false,
				// 用户导入参数
				upload: {
					// 是否显示弹出层（用户导入）
				 open: false,
					// 弹出层标题（用户导入）
					title: "用户导入",
					// 是否禁用上传
					isUploading: false,
					// 是否更新已经存在的用户数据
					updateSupport: false,
					// 设置上传的请求头部
					headers: {
						Authorization: "Bearer " + getToken()
					},
					// 上传的地址
					url: process.env.VUE_APP_BASE_API + "/viewLogin/user/importData"
				},
				// 查询参数
				queryParams: {
					pageNum: 1,
					pageSize: 10,
					name: '',
					phone: ''
				},
				// 表单参数
				form: {},
				btnLoading: false,
				// 表单校验
				rules: {
					name: [{
						required: true,
						message: "请输入姓名",
						trigger: "blur"
					}],
					phone: [{
						required: true,
						message: "请输入电话",
						trigger: "blur"
					}]
				},

			};
		},
		created() {
			this.getList();
		},
		methods: {
			// 提交上传文件
			submitFileForm() {
				this.$refs.upload.submit();
			},
			/** 下载模板操作 */
			importTemplate() {
				this.download('viewLogin/user/importTemplate', {}, `内部人员导入模板.xlsx`)
			},
			// 文件上传中处理
			handleFileUploadProgress(event, file, fileList) {
				this.upload.isUploading = true;
			},
			// 文件上传成功处理
			handleFileSuccess(response, file, fileList) {
				this.upload.open = false;
				this.upload.isUploading = false;
				this.$refs.upload.clearFiles();
				this.$alert("<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" +
					response.msg + "</div>", "导入结果", {
						dangerouslyUseHTMLString: true
					});
				this.getList();
			},
			handleUploadClose() {
				this.upload.open = false;
			},
			handleImport() { //导入
				this.upload.open = true;
			},
			/** 查询岗位列表 */
			getList() {
				this.loading = true;
				listPages(this.queryParams).then(response => {
					this.postList = response.rows;
					this.total = response.total;
					this.loading = false;
				});
			},
			// 取消按钮
			cancel() {
				this.open = false;
			},
			// 表单重置
			reset() {
				this.form = {
					name: undefined,
					phone: undefined
				};
				this.resetForm("form");
			},
			/** 搜索按钮操作 */
			handleQuery() {
				this.queryParams.pageNum = 1;
				this.getList();
			},
			/** 重置按钮操作 */
			resetQuery() {
				this.resetForm("queryForm");
				this.handleQuery();
			},
			// 多选框选中数据
			handleSelectionChange(selection) {
				this.ids = selection.map(item => item.id)
				this.single = selection.length != 1
				this.multiple = !selection.length
			},
			/** 新增按钮操作 */
			handleAdd() {
				this.reset();
				this.open = true;
				this.title = "新增";
			},
			/** 修改按钮操作 */
			handleUpdate(row) {
				this.reset();
				let postId = row.id || this.ids
				if (Array.isArray(postId)) postId = postId.toString()
				this.open = true;
				this.diLogloading = true
				this.title = "编辑";
				listDetails(postId).then(response => {
					this.form.id = response.data.id
					for (let i in this.form) this.form[i] = response.data[i]
					this.diLogloading = false
				});
			},
			/** 提交按钮 */
			submitForm: function() {
				this.$refs["form"].validate(valid => {
					if (valid) {
						this.btnLoading = true
						if (this.form.id != undefined) {
							listUpdate(this.form).then(response => {
								this.$modal.msgSuccess("编辑成功");
								this.open = false;
								this.btnLoading = false
								this.getList();
							});
						} else {
							listAdd(this.form).then(response => {
								this.$modal.msgSuccess("新增成功");
								this.open = false;
								this.btnLoading = false
								this.getList();
							});
						}
					}
				});
			},
			/** 删除按钮操作 */
			handleDelete(row) {
				let postIds = row.id || this.ids;
				if (Array.isArray(postIds)) postIds = postIds.toString()
				this.$modal.confirm('此操作将要删除所选的数据，是否继续？').then(function() {
					return listDelete(postIds);
				}).then(() => {
					this.getList();
					this.$modal.msgSuccess("删除成功");
				}).catch(() => {});
			}
		}
	};
</script>
